package com.vrv.common.resolver.dispatcher;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.vrv.common.resolver.RequestParameter;
import com.vrv.common.resolver.Response;
import com.vrv.common.resolver.locality.DBManager;
import com.vrv.common.utils.StringUtils;

/**
 * content://com.vrv.sqlite/dbname
 * 
 * @author 曹保利
 * 
 */
public class LocalityPageRequestHandler implements RequestState {

	@Override
	public Response handler(Context mContext, RequestParameter parameter, String[] detailKeys) {

		String uri = parameter.getUrl();
		String tableName = uri.substring(uri.lastIndexOf("/") + 1);
		String pageCount = "SELECT COUNT(*) from " + tableName + " WHERE 1=1 " + getFilterSql(parameter);
		String pageSql = "SELECT * from " + tableName + " WHERE 1=1 " + getFilterSql(parameter) + " LIMIT " + parameter.getPageSize() + " OFFSET "
		        + (parameter.getPageSize() * parameter.getPageNumber());

		Response response = new Response();
		try {
			ArrayList<Map<String, Object>> records = new ArrayList<Map<String, Object>>();
			int totalRecords = DBManager.getInstance(mContext).getCount(pageCount);
			DBManager.getInstance(mContext).open();
			SQLiteDatabase sqliteDb = DBManager.getInstance(mContext).getMSQLiteDatabase();
			Cursor mCursor = sqliteDb.rawQuery(pageSql, null);
			while (mCursor.moveToNext()) {
				Map<String, Object> varMap = new HashMap<String, Object>();
				for (int i = 0; i < detailKeys.length; i++) {
					int columnIndex = mCursor.getColumnIndex(detailKeys[i]);
					varMap.put(detailKeys[i], mCursor.getString(columnIndex));
				}
				records.add(varMap);
			}

			DBManager.getInstance(mContext).close();
			mCursor.close();
			response.setTotalRecords(totalRecords);
			response.setRecords(records);
			response.setSuccess(Boolean.TRUE);
			return response;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return response;
	}

	private String getFilterSql(RequestParameter parameter) {
		StringBuffer sql = new StringBuffer();
		// 处理排序
		Map<String, Boolean> sortMap = parameter.getSortMap();
		for (Iterator<String> iterator = sortMap.keySet().iterator(); iterator.hasNext();) {
			String key = iterator.next();
			sql.append(sortMap.get(key) ? " asc" : " desc");
		}
		// 处理like
		Map<String, String> lkeMap = parameter.getFilterLikeMap();
		for (Iterator<String> iterator = lkeMap.keySet().iterator(); iterator.hasNext();) {
			String key = iterator.next();
			String val = String.valueOf(lkeMap.get(key));
			if(!StringUtils.isNullOrEmpty(val)){
				sql.append(" AND ").append(key).append(" like '%").append(lkeMap.get(key)).append("%' ");
			}
		}
		// 处理 =
		Map<String, Object> args = parameter.getArgs();
		for (Iterator<String> iterator = args.keySet().iterator(); iterator.hasNext();) {
			String key = iterator.next();
			String val = String.valueOf(args.get(key));
			if (!StringUtils.isNullOrEmpty(val)) {
				if (!("pageSize".equals(key) || "pageNumber".equals(key))) {
					sql.append(" AND ").append(key).append(" = ").append(val);
					//sql.append(" AND ").append(key).append(" like '%").append(val).append("%' ");
				}
			}
		}
		return sql.toString();
	}
}
